<?php
session_start();
$page = 'Forum';

//Pagina in ontwikkeling
$style_nmr = (isset($_SESSION['style']) && $_SESSION['style'] > 1 ? $_SESSION['style'] : "");
$bestanden = "<link rel='stylesheet' type='text/css' href='/bestanden/js/popup/css/style_in_popup".$style_nmr.".css' />";
$no_style = true;
include_once($_SERVER['DOCUMENT_ROOT']."/header.php");
echo "<div class='information'><p class='icon'>Deze pagina is nog in ontwikkeling.</p></div>";
include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");
die();

include_once($_SERVER['DOCUMENT_ROOT']."/php_functions/database_connectie.php");

$error = "";
if(isset($_POST['toevoegen'])) {
	$goed = true;
	if(empty($_POST['titel']) || empty($_POST['bericht'])) {
		$error .= "<div class='error'><p class='icon'>Er is geen titel en/of bericht ingevoerd.</p></div>";
		$goed = false;
	}
	$titel = htmlentities($_POST['titel'], ENT_QUOTES);
	$bericht = htmlentities($_POST['bericht'], ENT_QUOTES);
	if(strlen($titel) > 100) {
		$error .= "<div class='error'><p class='icon'>De lengte van de titel is te lang. (".strlen($titel)."/100).</p></div>";
	}
	if(strlen($bericht) > 1000) {
		$error .= "<div class='error'><p class='icon'>De lengte van het bericht is te lang. (".strlen($bericht)."/1000).</p></div>";
	}
	
	if($error == "") {
		$result = mysql_query("INSERT INTO Forum (teamId, aangemaaktDoor, aangemaaktOp, titel)
						VALUES('".$_POST['teamId']."', '".$_SESSION['spelerId']."', NOW(), '".$titel."')");
		if($result) {
			$result = mysql_query("INSERT INTO Forumbericht (forumId, geschrevenDoor, geschrevenOp, bericht)
						VALUES(LAST_INSERT_ID(), '".$_SESSION['spelerId']."', NOW(), '".$bericht."')");
			if($result) {
				//Vraag de forumId van het laatst toegevoegde onderwerp		
				$result = mysql_query("SELECT forumId FROM Forum ORDER BY forumId DESC LIMIT 1");
				if($result) {
					//$bestanden = "<script type='text/javascript' src='/bestanden/js/post/js_post.js'></script>
					//	<script type='text/javascript'>window.onload = function() { post_to_url(\"./index.php\", {i:\"<div class='succes'><p class='icon'>Het onderwerp is succesvol toegevoegd.</p></div>\"})};</script>";
					$row = mysql_fetch_object($result);
					//header("Location: http://localhost/extra/forum/?forumId=".$row->forumId);
					$bestanden = "<script type='text/javascript' src='/bestanden/js/post/js_post.js'></script>
						<script type='text/javascript'>window.onload = function() { post_to_url(\"./?forumId=".$row->forumId."\", {})};</script>";
				}else {
					$bestanden = "<script type='text/javascript' src='/bestanden/js/post/js_post.js'></script>
						<script type='text/javascript'>window.onload = function() { post_to_url(\"./index.php\", {i:\"<div class='succes'><p class='icon'>Het onderwerp is succesvol toegevoegd.</p></div>\"})};</script>";
					//header("Location: http://localhost/extra/forum/?i=Het onderwerp is succesvol toegevoegd");
				}
			}else {
				$error .= "<div class='error'><p class='icon'>Er is iets mis gegaan tijdens het versturen van het bericht. Het bericht is niet toegevoegd.</p></div>";
			}
		}else {
			$error .= "<div class='error'><p class='icon'>Er is iets mis gegaan tijdens het versturen van het nieuwe onderwerp. Het onderwerp is niet toegevoegd.</p></div>";
		}
	}
}

if(isset($_POST['toevoegen']) && $error == "") {
	include_once($_SERVER['DOCUMENT_ROOT']."/php_functions/quick_post.php");
	die();
}else {
	$style_nmr = (isset($_SESSION['style']) && $_SESSION['style'] > 1 ? $_SESSION['style'] : "");
	$bestanden = "<link rel='stylesheet' type='text/css' href='/bestanden/css/style_tabellen".$style_nmr.".css' />
			<link rel='stylesheet' type='text/css' href='/bestanden/js/popup/css/style_in_popup".$style_nmr.".css' />
	<link rel='stylesheet' type='text/css' href='/bestanden/js/bbcode/css/write_bbcode.css' />
	<link rel='stylesheet' type='text/css' href='/bestanden/js/bbcode/css/read_bbcode.css' />
	<script type='text/javascript' src='/bestanden/js/bbcode/js/bbcode.js'></script>";

	$no_style = true;
	include_once($_SERVER['DOCUMENT_ROOT']."/header.php");
	echo $error;
	
	if(empty($_SESSION['spelerId'])) {
		echo "<div class='error'><p class='icon'>Login om deze pagina te kunnen bekijken.</p></div>";
		include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");
		die();
	}else {
		echo "<form action='onderwerpAanmaken.php' method='post'>";
			echo "<input type='hidden' name='teamId' value='3'>";
			
			echo "<div class='title'>";
				echo "<p>Titel: </p>";
				echo "<input name='titel' type='text' value='".str_replace("'", "&#39;", $_POST['titel'])."' size='50' maxlength='100'>";
			echo "</div>";
			
			echo "<div class='button_bar'>";
				echo "<div class='bbcode_buttons'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/bold.gif' onclick='insert_bbcode(\"[b]\", \"[/b]\", \"bericht\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/italic.gif' onclick='insert_bbcode(\"[i]\", \"[/i]\", \"bericht\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/underline.gif' onclick='insert_bbcode(\"[u]\", \"[/u]\", \"bericht\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/ordered.gif' onclick='insert_bbcode(\"[list=1][*]\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/unordered.gif' onclick='insert_bbcode(\"[list][*]\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/link.gif' onclick='insert_prompt_bbcode(\"[url]\", \"[/url]\", \"bericht\", \"Geef de URL op\", \"http://\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/image.gif' onclick='insert_prompt_bbcode(\"[img]\", \"[/img]\", \"bericht\", \"Geef de locatie van het plaatje op\", \"http://\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/quote.gif' onclick='insert_bbcode(\"[quote]\", \"[/quote]\", \"bericht\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/code.gif' onclick='insert_bbcode(\"[code]\", \"[/code]\", \"bericht\");'>";
				echo "</div>";
				echo "<div class='bbcode_emoticons'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/bigsmile.gif' onclick='insert_bbcode(\":D\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/smile.gif' onclick='insert_bbcode(\":)\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/laugh.gif' onclick='insert_bbcode(\"XD\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/tongue.gif' onclick='insert_bbcode(\":P\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/wink.gif' onclick='insert_bbcode(\":D\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/frown.gif' onclick='insert_bbcode(\":(\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/surprise.gif' onclick='insert_bbcode(\":O\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/worry.gif' onclick='insert_bbcode(\":s\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/sweatdrop.gif' onclick='insert_bbcode(\"^^\", \"\", \"bericht\");'>";    
				echo "</div>";
			echo "</div>";
			echo "<div class='clr'></div>";
			echo "<div class='textarea'>";
				echo "<textarea name='bericht' id='bericht' maxlength='1000'>".$_POST['bericht']."</textarea>";
			echo "</div>";
			echo "<span class='button'><input type='submit' name='voorbeeld' value='Voorbeeld'></span> ";
			echo "<span class='button'><input type='submit' name='toevoegen' value='Verder'></span>";
		echo "</form>";
		
		if(isset($_POST['voorbeeld'])) {
			include_once($_SERVER['DOCUMENT_ROOT']."/php_functions/nbbc_parser/nbbc.php");
			$bbcode = new BBCode;
			$bbcode->SetAllowAmpersand(true);
			$bbcode->SetSmileyURL("/images/style".$style_nmr."/emoticons");
			
			echo "<div class='voorbeeld'>";
				echo "<p>Voorbeeld<p>";
				echo "<div class='entry'>";
					echo $bbcode->Parse($_POST['bericht']);
				echo "</div>";
			echo "</div>";
		}
	}
}
include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");
?>